﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SearchClient.ServiceClientReference;

namespace SearchClient
{
    public partial class MainPage : UserControl
    {
        private ServiceClientClient service = new ServiceClientClient();
        private int indexVideo = 0;
        private List<VideoInstitucional> videos = new List<VideoInstitucional>();
        private Usuario usuario = null;
        //private string pathVideos;

        public MainPage(Usuario usuarioLog)
        {
            InitializeComponent();
            usuario = usuarioLog;
            CargarVideosInstitucionales();

        }

        private void CargarVideosInstitucionales()
        {
            service.GetVideosInstitucionalesCompleted += new EventHandler<GetVideosInstitucionalesCompletedEventArgs>(CargarVideosInstitucionalesCompleted_Event);
            try
            {
                service.GetVideosInstitucionalesAsync();
            }
            catch (Exception)
            {
            }
        }

        void CargarVideosInstitucionalesCompleted_Event(object sender, GetVideosInstitucionalesCompletedEventArgs e)
        {
            videos = e.Result.ToList();
            if (videos.Count > 0)
            {
                videosInstitucionalesPlayer.Source = new Uri(videos[indexVideo].Path);
                videosInstitucionalesPlayer.Play();
                indexVideo++;
            }
        }

        private void videosInstitucionalesPlayer_MediaEnded(object sender, RoutedEventArgs e)
        {
            videosInstitucionalesPlayer.Source = new Uri(videos[indexVideo].Path);
            videosInstitucionalesPlayer.Play();
            indexVideo++;
            if (indexVideo == videos.Count)
            {
                indexVideo = 0;
            }
        }

        private void btnBusqueda_Click(object sender, RoutedEventArgs e)
        {
            service.BuscarFileSystemElementsCompleted += new EventHandler<BuscarFileSystemElementsCompletedEventArgs>(BuscarFileSystemElementsCompleted_Event);

            try
            {
                service.BuscarFileSystemElementsAsync(txtBusqueda.Text);
            }
            catch (Exception)
            {
            }
        }

        void BuscarFileSystemElementsCompleted_Event(object sender, BuscarFileSystemElementsCompletedEventArgs e)
        {
            List<FileSystemElement> fseList = e.Result.ToList();

            fseGrid.ItemsSource = fseList;
        }
    }
}
